package com.groupnections.client.LiveFeed;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.groupnections.client.GlobalVariables;
import com.groupnections.client.Entities.InitializeEntity;
import com.groupnections.shared.Entity;
import com.groupnections.shared.LiveFeed;

public class ListFeedPanel extends VerticalPanel {

	public ListFeedPanel(Entity entity, LiveFeed[] liveFeeds) {

		this.setSize("100%", "100%");

		for (LiveFeed liveFeed : liveFeeds) {

			HorizontalPanel feed = new HorizontalPanel();

			feed.setSpacing(7);

			String nameEntityOther = liveFeed.getEntityFrom().getName();

			if (entity.getEntityID().equals(nameEntityOther)) {

				nameEntityOther = liveFeed.getEntityTo().getName();
			}

			Entity entityOther = GlobalVariables.entitiesMap
					.get(nameEntityOther);

			feed.add(getLiveFeedImage(entityOther));

			feed.add(getFeedDescription(entityOther,
					liveFeed.getEntityDescription()));

			this.add(feed);
		}
	}

	private Anchor getLiveFeedImage(final Entity entity) {

		Anchor image = new Anchor("<img src='" + GlobalVariables.IMAGE_FOLDER
				+ entity.getEntityID() + ".jpg' width='44px' heigth='37px' >",
				true);

		image.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				new InitializeEntity(entity);
			}
		});

		return image;
	}

	private HTML getFeedDescription(final Entity entity, String feedDescription) {

		HTML name = new HTML(feedDescription);
		name.setWidth("300px");

		name.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				new InitializeEntity(entity);
			}
		});

		return name;
	}
}
